package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.x509.CRLDistributionPointsExtension;
import sun.security.x509.DistributionPoint;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 12/4/11
 * Time: 5:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class ECrlDistributionPointsExtension implements ECertExtension{
    CRLDistributionPointsExtension crlDistributionPointsExtension;
    @Override
    public String getName() {
        return Bundle.getString("ECrlDistributionPointsExtension.Name");
    }

    @Override
    public String getShortTitle() {
        StringBuilder sb = new StringBuilder();
        try {
                List<DistributionPoint> dps = null;
                dps = (List<DistributionPoint>) crlDistributionPointsExtension.get(CRLDistributionPointsExtension.POINTS);
                String locationStr;
                for(int i=0; i<dps.size(); i++){
                    sb.append( ""+(i+1) );
                    sb.append(")");
                    locationStr = dps.get(i).getFullName().get(0).getName().toString();
                    sb.append( locationStr.substring(locationStr.indexOf(':') + 1) );
                    sb.append("\n");
                }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return sb.toString();
    }

    @Override
    public String getLongTitle() {
        return getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return crlDistributionPointsExtension.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof CRLDistributionPointsExtension)
        {
            crlDistributionPointsExtension = (CRLDistributionPointsExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
